//如果想要发送异步请求，我们需要一个关键的对象 XMLHttpRequest
var xmlHttpRequest ;

function createXMLHttpRequest(){
    if(window.XMLHttpRequest){
        //符合DOM2标准的浏览器 ，xmlHttpRequest的创建方式
        xmlHttpRequest = new XMLHttpRequest() ;
    }else if(window.ActiveXObject){//IE浏览器
        try{
            xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        }catch (e) {
            xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP")
        }
    }
}

const second = 60;
let delay = second;
let timer = null;
let btn = null;

function fn(){
    if(delay != 0){
        btn.innerText = delay+'s';
        btn.style.backgroundColor = "#999"
        delay--;
    }else {
        clearInterval(timer);
        btn.innerText = "重新获取";
        // btn.style.backgroundColor = "orange";
        btn.style.background="orange";
        delay = second;
        btn.disabled = false
    }
    return fn
}


function getCodeByPhone(){

    createXMLHttpRequest();
    var telephone;
    var form=document.getElementById("phone_data");
    var formData=new FormData(form);
    telephone = formData.get("telephone");

    btn = document.querySelector("button.btn_phone");
    // btn.onclick=function (){
    btn.disabled = true;
    timer = setInterval(fn(),1000);
    // }

    var url = "getLoginByPhone/"+telephone;
    xmlHttpRequest.open("GET",url,true);
    //设置回调函数  当状态有改变的时候，就去调用回调函数（chUnameCB）
    xmlHttpRequest.onreadystatechange = getCodeAjax ;
    //发送请求
    xmlHttpRequest.send();

}


function getCodeAjax(){

    if(xmlHttpRequest.readyState==4 && xmlHttpRequest.status==200){
        //xmlHttpRequest.responseText 表示 服务器端响应给我的文本内容
        //alert(xmlHttpRequest.responseText);
        var responseText = xmlHttpRequest.responseText ;// {'uname':'1'}
        // alert(responseText);
    }
}